// pages/query/query.js
import {request} from "../../utils/request";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 隐藏历史搜索数据
    historyLabel: true,
    // 搜索框数据
    iptValue: '',
    // 商品数据
    commodityList: [],
    // 控制显示
    pageShow: true,
    // placeholder 的值
    placeholderValue: "疯狂星期四",
    // 历史搜索记录数据
    historyList: [],
    // 热门搜索标签
    hotSearchList: [
      '培根蛋法风烧饼','熏鸡法风烧饼','乳酪熏鸡云朵汤种吐司','乳酪花生云朵汤种吐司','芝士猪柳蛋帕尼尼','芝士猪柳帕尼尼','K记小笼包(6只)','大饼卷香烤鸡扒蛋','奶芝意面汁汁和牛堡','藤椒风味鸡排饼汉堡','黄金鸡块(10块)','疯狂星期四'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.inputControl();

    let key = wx.getStorageSync('historyAry');
    console.log("key: ", key.length);
    if(key.length > 0){
      this.setData({
        historyList: key
      });
    }

    if(this.data.historyList.length <= 0){
      this.setData({
        historyLabel: false
      });
    }

    setInterval(() => {
      this.inputControl();
    }, 5000);
  },

  // 清除历史记录
  clearHistory(){
    this.setData({
      historyList: [],
      historyLabel: false
    });
  },

  // 修改并获取输入框的值
  changeVaule(e){
    let iptValue = e.detail.value;
    this.setData({iptValue});
  },

  // 搜索方法
  async search(){

    let ary = [];
    if(this.data.iptValue.length > 0){
      ary = [...this.data.historyList, this.data.iptValue]; //[...this.historyList, this.data.iptValue]
    }
    this.setData({
      historyList: ary
    });

    wx.setStorageSync('historyAry', this.data.historyList);

    console.log("本地存储数据： ", wx.getStorageSync('historyAry'));

    this.setData({
      historyLabel: true
    });

    if(this.data.iptValue.length <= 0){
      //
      this.setData({
        pageShow: true
      });
      wx.showToast({title: '名字不能为空！'});
      return;
    }
    // 请求的信息 (请求前的准备)
    let requestData = {
      url: '/queryCommoditys',
      header: {'content-type': "application/x-www-form-urlencoded"},
      method: 'post',
      data: {'commodityName': this.data.iptValue}
    };

    // 开始请求
    let {data: res} = await request(requestData);
    
    if(res[0].status != 400){
     let {data: commodityList} = res[0];

     // 切换到商品区
     this.setData({
      pageShow: false
    });

     this.setData({commodityList});
    } else {
      this.setData({
        pageShow: true
      });
      wx.showToast({title: '暂时没有该商品'});
      return;
    }
    console.log(this.data.commodityList);
    console.log('程序到此结束...');
  },

  // 控制input框的预选文本
  inputControl(){
    let placeholderValueAry = ['培根蛋法风烧饼','熏鸡法风烧饼','乳酪熏鸡云朵汤种吐司','乳酪花生云朵汤种吐司','芝士猪柳蛋帕尼尼','芝士猪柳帕尼尼','K记小笼包(6只)','大饼卷香烤鸡扒蛋','奶芝意面汁汁和牛堡','藤椒风味鸡排饼汉堡','老北京风味饼汉堡','新奥尔良烤鸡腿堡','香辣鸡腿堡 (辣)','劲脆鸡腿堡','老北京鸡肉卷','滋滋YES烤鸡腿堡','黄金SPA鸡排堡(藤椒风味)','深海鳕鱼堡','双层深海鳕鱼堡','汁汁厚作培根和牛堡','汁汁厚作培根和牛堡双','汁汁厚作培根和牛堡三层','汁汁双层嫩牛堡（经典美式风味）','汁汁双层嫩牛堡（烟熏辣椒风味）','经典美式风味嫩牛堡（单层）','疯狂星期四','蜜汁全鸡','港式烧味脆皮大鸡腿饭','整切西冷牛排(1袋，非即食)','黄金鸡块(10块)培根蛋法风烧饼','熏鸡法风烧饼','乳酪熏鸡云朵汤种吐司','乳酪花生云朵汤种吐司','芝士猪柳蛋帕尼尼','芝士猪柳帕尼尼','K记小笼包(6只)','大饼卷香烤鸡扒蛋','奶芝意面汁汁和牛堡','藤椒风味鸡排饼汉堡','老北京风味饼汉堡','新奥尔良烤鸡腿堡','香辣鸡腿堡 (辣)','劲脆鸡腿堡','老北京鸡肉卷','滋滋YES烤鸡腿堡','黄金SPA鸡排堡(藤椒风味)','深海鳕鱼堡','双层深海鳕鱼堡','汁汁厚作培根和牛堡','汁汁厚作培根和牛堡双','汁汁厚作培根和牛堡三层','汁汁双层嫩牛堡（经典美式风味）','汁汁双层嫩牛堡（烟熏辣椒风味）','经典美式风味嫩牛堡（单层）','疯狂星期四','蜜汁全鸡','港式烧味脆皮大鸡腿饭','整切西冷牛排(1袋，非即食)','黄金鸡块(10块)'];

    let idx = Math.round(Math.random() * (placeholderValueAry.length - 1));
      let footName = placeholderValueAry[idx];
      this.setData({
        placeholderValue: footName
      });
  }
})